package com.pdsu.xpf.graduation01.control;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.pdsu.xpf.graduation01.domain.Commodity;
import com.pdsu.xpf.graduation01.domain.CommodityType;
import com.pdsu.xpf.graduation01.domain.Type;
import com.pdsu.xpf.graduation01.service.impl.CommodityTypeServiceImpl;
import com.pdsu.xpf.graduation01.utils.JsonFormat;
import com.pdsu.xpf.graduation01.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/commodityType")
public class CommodityTypeControl {

    final
    CommodityTypeServiceImpl commodityTypeService;

    public CommodityTypeControl(CommodityTypeServiceImpl commodityTypeService) {
        this.commodityTypeService = commodityTypeService;
    }

    @ResponseBody
    @RequestMapping("/getTypeList")
    public Result getTypeList(HttpServletRequest request) throws JsonProcessingException {

        List<Type> types=commodityTypeService.getTypeList();

        String commodities_json= JsonFormat.ListToJson(types);
        System.out.println(commodities_json);
        return Result.success(commodities_json);
    }
    @ResponseBody
    @RequestMapping("/getCommodityListByTID")
    public Result getCommodityListByTID(HttpServletRequest request,int tid) throws JsonProcessingException {

        System.out.println(tid+"tid");


        List<CommodityType> commodityTypes=commodityTypeService.getCommodityListByTID(tid);
        String commodities_json= JsonFormat.ListToJson(commodityTypes);
        System.out.println(commodities_json);
        return Result.success(commodities_json);
    }


}
